package com.gelsanalyzer.application;

import java.awt.*;
import java.beans.*;

import javax.swing.*;

import org.apache.log4j.*;

import com.gelsanalyzer.business.*;
import com.gelsanalyzer.mvc.controller.*;
import com.gelsanalyzer.mvc.model.*;
import com.gelsanalyzer.mvc.view.*;

public class CourseEntryController extends AbstractController {
    private static final Logger log = Logger
        .getLogger(CourseEntryController.class);

    public CourseEntryController(IView courseEntryView,
                                 AbstractModel courseEntryController) {
        super(courseEntryView, courseEntryController);
        log.info("about to inject model/view dependancies");
        ((CourseEntryModel) model)
            .setAbstractTableModel( ((CourseEntryView) view).getTableModel());
        log.info("done constructing");
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        log.info("propertyChanged:" + evt);
        // super.propertyChange(evt);
        if ( evt.getPropertyName().equals(IController.APP_ADDED_COURSE) ) {
            CourseEntryModel cem = (CourseEntryModel) model;
            CourseEntryList cel = (CourseEntryList) cem.getAbstractTableModel();  
            
            if(evt.getNewValue()==null) {                
                JOptionPane.showMessageDialog((Component) parent.getView(),
                    "Can not add null course,"
                            + " please enter another course.", "Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
                
            
            if ( cel.getIndexof(new CourseEntryItem((Course) evt.getNewValue(),
                null)) >= 0 ) {
                JOptionPane.showMessageDialog((Component) parent.getView(),
                    "Course already appears in"
                            + " list, please enter another course.", "Error",
                    JOptionPane.ERROR_MESSAGE);
            } else {
                // cel.addCourseEntryItem(CEI);               
                setModelProperty(evt.getPropertyName(), evt.getNewValue());
            }
        }
        else if( evt.getPropertyName().equals(IController.APP_REMOVED_COURSE) ) {
            CourseEntryModel cem = (CourseEntryModel) model;
            CourseEntryList cel = (CourseEntryList) cem.getAbstractTableModel(); 
            if(evt.getNewValue()==null) {
                JOptionPane.showMessageDialog((Component) parent.getView(),
                    "Can not remove null course,"
                            + " please enter another course.", "Error",
                    JOptionPane.ERROR_MESSAGE);
                return;
            }
        
            if( cel.getIndexof(new CourseEntryItem((Course) evt.getNewValue(), null)) <0) {
                JOptionPane.showMessageDialog((Component) parent.getView(), 
                    "Course not in list, cannot remove, " +
                    "please enter another course.", "ERROR", JOptionPane.ERROR_MESSAGE);
            } else {
                setModelProperty(evt.getPropertyName(), evt.getNewValue());
            }
        }
   }
    

    @Override
    protected void setModelProperty(String propertyName, Object newValue) {
        log.info("setModelProperty(" + propertyName + "," + newValue + ")");
        super.setModelProperty(propertyName, newValue);
    }

}
